<?php

namespace SinoPHP\BbdBoss\Api;

use InvalidArgumentException;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Upfor\UpforPHP\Library\Curl\Curl;

/**
 * IP变更
 *
 * Todo: 本接口尚未实现业务, 暂仅作为对外调试连通性
 *
 * Params:
 *      data:id         (integer)      IP对应ID
 *      data:oldIp      (string)       原IP
 *      data:newIp      (string)       新IP
 */
class CheckIpIsDisabled extends ApiAbstract {

    const BUILD_CDN = 0;
    const MIX_CDN = 1;

    public function v_1_0_0() {
        $params = $this->requestParams;
        //$this->checkToken(); // 校验TOKEN
        $result = ['flag' => 0, 'msg' => ''];
        //参数必须是数组
        if (!is_array($params[self::BUILD_CDN]) || !is_array($params[self::MIX_CDN])) {
            throw new InvalidArgumentException('', 103002);
        }
        //参数不能为空
        if (empty($params[self::BUILD_CDN]) && empty($params[self::MIX_CDN])) {
            throw new InvalidArgumentException('', 103004);
        }
        $buildBoss = [];

        foreach ($params[self::BUILD_CDN] as $key => $value) {
            if (empty($key)) {
                throw  new InvalidOptionException('id:' . $key . '不正确', 102001);
            }
            if (empty($value) || !filter_var($value, FILTER_VALIDATE_IP)) {
                throw new InvalidArgumentException('ip:' . $value . '不正确', 103001);
            }
            $buildBoss[] = $value;
        }
        $mixBoss = [];
        foreach ($params[self::MIX_CDN] as $key => $value) {
            if (empty($key)) {
                throw  new InvalidOptionException('id:' . $key . '不正确', 102001);
            }
            if (empty($value) || !filter_var($value, FILTER_VALIDATE_IP)) {
                throw new InvalidArgumentException('ip:' . $value . '不正确', 103001);
            }
            $mixBoss[] = $value;
        }

        if ($buildBoss) {
            //自建判断是否可以暂停
            $where['is_delete'] = 0;
            $where['ip'] = $buildBoss;
            $ipData = model('ServerIp')->getList('ip,ip_id', $where);
            $ipSortById = [];
            foreach ($ipData as $value) {
                $ipSortById[$value['ip_id']] = $value['ip'];
            }
            $ipList = array_column($ipData, 'ip');
            $differentList = array_diff($buildBoss, $ipList);
            if ($differentList) {
                throw new InvalidArgumentException(implode(',', $differentList) . ' 在数据库里不存在', 103005);
            }
            //todo
            //平台域名使用IP 判断

            $ipIds = array_column($ipData, 'ip_id');
            $status = model('BasicInformation')->checkIsUsed($ipIds);

            if ($status['status'] === false) {
                $result['msg'] = $ipSortById[$status['ip_id']] . '已被使用';
                $this->success($result);
            }
        }

        if ($mixBoss) {
            //融合boss判断是否可以暂停
            $url = config('bossUrl', 'outApi');
            $curl = new Curl();
            $curl->setTimeout(3);
            $curl->post($url . '/outApi/CheckIpIsDisabled', ['data' => $mixBoss]);
            if ($curl->error) {
                $this->success(['flag' => 0]);
            }
            $curl->close();
            $data = @json_decode($curl->rawResponse, true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                $this->success($result);
            }
            if ($data['ret'] == 103005) {
                throw new InvalidArgumentException($data['msg'], 103005);
            }
            if ($data['data']['status'] === false) {
                $result['msg'] = $data['data']['msg'];
                $this->success($result);
            }
        }
        $result['flag'] = 1;
        $this->success($result);

    }

}
